//
//  BIDCustomDateView.m
//  Nav
//
//  Created by mal on 13-10-31.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDCustomDateView.h"
#import "UIView+FindFirstResponder.h"

@interface BIDCustomDateView()
{
    int _mode;
}
@end

@implementation BIDCustomDateView
@synthesize datePicker;
@synthesize confirmBtn;
@synthesize cancelBtn;
@synthesize toolBar;
@synthesize bShow;
@synthesize delegate;
@synthesize bIsInputView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (id)initWithMode:(NSUInteger)mode
{
    self = [super init];
    if(self)
    {
        _mode = (int)mode;
        [self initView];
    }
    return self;
}

- (void)initView
{
    bShow = NO;
    [self setBackgroundColor:[UIColor whiteColor]];
    self.alpha = 1.0f;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGRect ownFrame = CGRectMake(0, screenSize.height, screenSize.width, 246.0f);
    self.frame = ownFrame;
    CGRect datePickerFrame = CGRectMake(0.0f, 30.0f, self.frame.size.width, self.frame.size.height-30.0f);
    self.datePicker = [[UIDatePicker alloc] initWithFrame:datePickerFrame];
    if(_mode==0)
    {
        self.datePicker.datePickerMode = UIDatePickerModeTime;
    }
    else if(_mode==1)
    {
       self.datePicker.datePickerMode = UIDatePickerModeDate;
    }
    else if(_mode==2)
    {
        self.datePicker.datePickerMode = UIDatePickerModeDate;
    }
    else if(_mode==3)
    {
        self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    }
    [self addSubview:datePicker];
    
    CGSize toolBarSize = CGSizeMake(ownFrame.size.width, 44.0f);
    CGRect toolBarFrame = CGRectMake(0, 0, toolBarSize.width, toolBarSize.height);
    toolBar = [[UIToolbar alloc] initWithFrame:toolBarFrame];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelBtnDown)];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(confirmBtnDown)];
    item1.tintColor = [UIColor whiteColor];
    item2.tintColor = [UIColor whiteColor];
    item3.tintColor = [UIColor whiteColor];
    [toolBar setItems:[[NSArray alloc] initWithObjects:item1, item2, item3, nil]];
    toolBar.barStyle = UIBarStyleBlack;
    toolBar.tintColor = [UIColor whiteColor];
    [self addSubview:toolBar];
}

- (void)show
{
    if(!bShow)
    {
        bShow = YES;
        UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
        
        __block CGRect frame1 = self.frame;
        [keyWindow addSubview:self];
        [UIView animateWithDuration:0.6f animations:^{frame1.origin.y = [UIScreen mainScreen].bounds.size.height-246; self.frame = frame1;}];
    }
}

- (void)dismissView
{
    bShow = NO;
    __block CGRect frame1 = self.frame;
    [UIView animateWithDuration:0.35f animations:^{frame1.origin.y=[UIScreen mainScreen].bounds.size.height;self.frame = frame1;} completion:^(BOOL finished){[self removeFromSuperview];}];
}

//确定按钮事件
- (void)confirmBtnDown
{
    NSDate *curDate = [self.datePicker date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    if(_mode==0)
    {
        [formatter setDateFormat:@"HH:mm"];
    }
    else if(_mode==1)
    {
        [formatter setDateFormat:@"yyyy-MM-dd"];
    }
    else if(_mode==2)
    {
        [formatter setDateFormat:@"yyyy-M-d"];
    }
    else if(_mode==3)
    {
        [formatter setDateFormat:@"yyyy-MM-dd H:mm"];
    }
    NSString *date = [formatter stringFromDate:curDate];
    [delegate getDate:date];
    if(bIsInputView)
    {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        UITextField *tf = (UITextField*)[keyWindow findFirstResponder];
        [tf resignFirstResponder];
    }
    else
    {
        [self dismissView];
    }
}

//取消按钮事件
- (void)cancelBtnDown
{
    if(bIsInputView)
    {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        UITextField *tf = (UITextField*)[keyWindow findFirstResponder];
        [tf resignFirstResponder];
    }
    else
    {
        [self dismissView];
    }
    if([delegate respondsToSelector:@selector(doCancel)])
    {
        [delegate doCancel];
    }
}

@end
